//
//  ZWHNewfriendViewController.swift
//  约麻
//
//  Created by Syrena on 2017/7/20.
//  Copyright © 2017年 Sweet. All rights reserved.
//

import UIKit
import AVOSCloud
import MJRefresh

class ZWHNewfriendViewController: UIViewController {
    
    var dataArray = [[String:String]]()

    @IBOutlet weak var tableview: UITableView!
    
    override func viewWillAppear(_ animated: Bool) {
        getrequest()
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        tableview.register(UINib.init(nibName: "ZWHNewTableViewCell", bundle: nil), forCellReuseIdentifier: "ZWHNewTableViewCell")
        setnavigation()
        
        tableview.mj_header = MJRefreshNormalHeader(refreshingBlock: { 
            self.getrequest()
        })
    }
    //获取请求
    func getrequest(){
        dataArray.removeAll()
        guard let arrayM = AVUser.current()?.object(forKey: "requestArr") as? [[String:String]] else{
            self.tableview.mj_header.endRefreshing()
            return
        }
        dataArray += arrayM
        tableview.reloadData()
        self.tableview.mj_header.endRefreshing()
        
    }
    
    //设置导航栏
    func setnavigation(){
        title = "新麻友"
        navigationController?.navigationBar.barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //定义标题颜色与字体大小字典
        let dict:NSDictionary = [NSForegroundColorAttributeName: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1),NSFontAttributeName : UIFont.boldSystemFont(ofSize: 13)]
        //标题设置颜色与字体大小
        
        navigationController?.navigationBar.titleTextAttributes = dict as? [String : AnyObject]
        
        let Btn = UIButton(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
        Btn.setImage(UIImage.init(named:"黑色返回"), for: .normal)
        Btn.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
        let back = UIBarButtonItem(customView: Btn)
        navigationItem.leftBarButtonItem = back
        
        
        
        
    }
    func backClicked(){
        navigationController?.popViewController(animated: true)
    }
    
    
    @IBAction func searchFriend(_ sender: UIButton) {
        let vc = UIStoryboard(name: "ZWHSearchViewController", bundle: nil).instantiateViewController(withIdentifier: "ZWHSearchViewController")
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension ZWHNewfriendViewController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return "新的麻友"
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ZWHNewTableViewCell", for: indexPath) as? ZWHNewTableViewCell
        cell?.dict = dataArray[indexPath.row]
        cell?.delegate = self
        cell?.page = indexPath.row
        return cell!
    }
}


extension ZWHNewfriendViewController:ZWHNewTableViewCellDelegate{
    func addfriend(page: Int) {
        dataArray.remove(at: page)
        AVUser.current()?.setObject(dataArray, forKey: "requestArr")
        AVUser.current()?.saveInBackground({ [weak self](success, error) in
            if success == true{
                ZWHAlertController.showAlert(vc: self!, title: "同意成功", message: nil, style: .alert)
                self?.tableview.reloadData()
            }
        })
    }
}
